package com.swipemaster;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

class Panel extends SurfaceView implements SurfaceHolder.Callback
{
	public static int MAX_NUMBER_OF_ELEMENTS = 1000;
	private PainterThread mThread;
	private Element[] elements = new Element[MAX_NUMBER_OF_ELEMENTS];
	Bitmap texture = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
	public final int width = texture.getHeight();
	public final int height = texture.getWidth();
	public final int halfOfWidth = width / 2;
	public final int halfOfHeight = height / 2;
	public int screenWidth;
	public int screenHeight;
	final Paint paint = new Paint();
	int numberOfElements = 0;

	public Panel(Context context)
	{
		super(context);
		getHolder().addCallback(this);
		mThread = new PainterThread(getHolder(), this);
		setFocusable(true);
	}

	@Override
	public void onDraw(Canvas canvas)
	{
		canvas.drawColor(Color.WHITE);
		for (int i = 0; i < numberOfElements; i++)
		{
			Element element = elements[i];
			canvas.drawBitmap(element.bitmap, element.x, element.y, paint);
		}
		canvas.drawText(Integer.toString(numberOfElements), 10, 10, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		synchronized (mThread.getSurfaceHolder())
		{
			int x = (int) event.getX();
			int y = (int) event.getY();
			if (x - halfOfWidth < 0 || x + halfOfWidth > screenWidth || y - halfOfHeight < 0 || y + halfOfHeight > screenHeight)
			{
				return true;
			}
			Element element = new Element(texture, x - halfOfWidth, y - halfOfHeight);
			elements[numberOfElements] = element;
			numberOfElements++;
			return true;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		mThread.setRunning(true);
		mThread.start();
		screenWidth = getWidth();
		screenHeight = getHeight();
	}

	/**
	 * We have to tell thread to shut down & wait for it to finish, or else it might touch the Surface after we return
	 * and explode.
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		boolean retry = true;
		mThread.setRunning(false);
		while (retry)
		{
			try
			{
				mThread.join();
				retry = false;
			}
			catch (InterruptedException e)
			{
				// We will try it again and again...
			}
		}
	}

	public void updatePhysics()
	{
		for (int i = 0; i < numberOfElements; i++)
		{
			Element element = elements[i];
			// Move.
			element.x += element.speedX;
			element.y += element.speedY;
			// Borders for x.
			if (element.x < 0 || element.x + width > screenWidth)
			{
				element.speedX = -element.speedX;
			}
			// Borders for y.
			if (element.y < 0 || element.y + height > screenHeight)
			{
				element.speedY = -element.speedY;
			}
		}
	}
}